package com.mindata.blockchain.core.sqlparser;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.google.common.collect.Maps;
import com.mindata.blockchain.block.Block;
import com.mindata.blockchain.block.Instruction;
import com.mindata.blockchain.block.Operation;
import com.mindata.blockchain.common.CommonUtil;
import com.mindata.blockchain.common.FastJsonUtil;
import com.mindata.blockchain.core.model.OrderEntity;
import com.mindata.blockchain.core.model.OrderTrackEntity;
import com.mindata.blockchain.core.model.TicketEntity;
import com.mindata.blockchain.core.repository.OrderRepository;
import com.mindata.blockchain.core.repository.OrderTrackRepository;
import com.mindata.blockchain.core.repository.TicketRepository;
import com.mindata.blockchain.core.requestbody.BlockRequestBody;
import com.mindata.blockchain.core.requestbody.InstructionBody;
import com.mindata.blockchain.core.service.BlockService;
import com.mindata.blockchain.core.service.InstructionService;
import org.apache.commons.compress.utils.Lists;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 王鹏
 */
@Service
public class TicketSqlParser extends AbstractSqlParser<TicketEntity>{
    @Resource
    private TicketRepository ticketRepository;
    @Resource
    private OrderTrackRepository orderTrackRepository;

    @Resource
    private InstructionService instructionService;

    @Resource
    private OrderRepository orderRepository;

    @Resource
    private BlockService blockService;

    @Value("${publicKey:A8WLqHTjcT/FQ2IWhIePNShUEcdCzu5dG+XrQU8OMu54}")
    private String publicKey;
    @Value("${privateKey:yScdp6fNgUU+cRUTygvJG4EBhDKmOMRrK4XJ9mKVQJ8=}")
    private String privateKey;

//    @Value("${spring.profiles.active}")
//    private String activeProfile;


    @Override
    void parse(byte operation, String messageId, TicketEntity entity) {
        try {
            if (Operation.ADD == operation) {
                entity.setCreateTime(CommonUtil.getNow());
                entity.setMessageId(messageId);
                ticketRepository.save(entity);
            } else if (Operation.UPDATE == operation) {
                TicketEntity ticketEntity = ticketRepository.findByMessageId(messageId);
                BeanUtil.copyProperties(entity, ticketEntity, CopyOptions.create().setIgnoreNullValue(true).setIgnoreProperties("id", "createTime"));
                ticketRepository.save(ticketEntity);
            if(entity.getStatus() == 2 ){
                // todo 如果环境是订单，进行更新
                OrderEntity order = orderRepository.findByTicketId(ticketEntity.getMessageId());

                OrderTrackEntity orderTrack = new OrderTrackEntity();
                orderTrack.setOrderId(order.getOrderId());
                orderTrack.setOrderStatus(2);
                orderTrack.setCreateTime(CommonUtil.getNow());
                orderTrackRepository.save(orderTrack);
//                try {
//                    // todo 订单打包块，广播给所有人，订单状态为出票成功
//                    BlockRequestBody blockRequestBody = new BlockRequestBody();
//                    blockRequestBody.setPublicKey(publicKey);
//                    com.mindata.blockchain.block.BlockBody blockBody = new com.mindata.blockchain.block.BlockBody();
//                    InstructionBody orderInstructionBody = new InstructionBody();
//                    orderInstructionBody.setOperation(Operation.UPDATE);
//                    orderInstructionBody.setTable("order_main");
//                    Map<String, Object> orderMap = Maps.newHashMapWithExpectedSize(4);
//                    orderMap.put("status", 2);
//                    orderInstructionBody.setInstructionId(order.getOrderId());
//                    orderInstructionBody.setJson(FastJsonUtil.toJSONString(orderMap));
//                    orderInstructionBody.setPublicKey(publicKey);
//                    orderInstructionBody.setPrivateKey(privateKey);
//                    Instruction orderInstruction = instructionService.build(orderInstructionBody);
//                    List<Instruction> instructionList = Lists.newArrayList();
//                    instructionList.add(orderInstruction);
//                    blockBody.setInstructions(instructionList);
//                    blockRequestBody.setBlockBody(blockBody);
//                    Block block = blockService.addBlock(blockRequestBody);
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
            }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    Class getEntityClass() {
        return TicketEntity.class;
    }
}
